<?php 
/*
 * @Descripttion: 同步政府接口
 * @version: v1
 * @Author: Terry
 * @Date: 2020-09-15 16:27:31
 * @LastEditors: Terry
 * @LastEditTime: 2024-05-27 18:36:04
 */
namespace libs;
use think\facade\Db;


class SwooleAsync {

    private $domain = "127.0.0.1";
    private $client;
    /**
     * @description: 构造函数，获取项目的配置（对接什么政府对应的配置）
     * @Author: Terry
     * @Date: 2022-04-20 19:25:46
     * @param {*} $prjCode
     */    
    public function __construct(){
        $this->client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
        $this->domain = env('swoole.domain');
    }

    /**
     * @description: 可自定义发送的数据
     * @Author: Terry
     * @Date: 2022-05-20 17:03:49
     * @param $data 发送的数据
     */    
    private function send($data){
        $this->client->send(json_encode($data).PHP_EOL);
    }
    /**
     * @description: 进行政府数据同步
     * @Author: Terry
     * @Date: 2022-05-12 10:34:43
     * @param string $prjCode 项目唯一码
     * @param string $module 模块 
     * @param string $func 
     * @param array $data 
     */    
    public function governmentSync($prjCode,$module,$func,$data){
        $configData = DB::table('platform_projectGovernmentConfig')
        ->where([
            'projectCode'=>$prjCode,
            'isDelete'=>0,
            'module'=>$module,
        ])->field(['type','governmentPrjCode','appid','appSecret','intergratorUuid'])->select();
        if(empty($configData)) $this->send([]);;
        $ret = $this->client->connect($this->domain, 9000);
        if(!empty($ret)){
            foreach($configData  as $key => $value){
                $swooleData = compact('prjCode','module','func','data','value');
                $this->send($swooleData);
            }
            $this->client->close();
        }
    }

    /**
     * @description: 微信通知
     * @Author: Terry
     * @Date: 2022-05-12 15:50:15
     * @param string $wxappUserUuid 用户唯一码
     * @param string $type 发送模板的类型
     * @param array $data 模板信息
     * @param array $params 额外的参数，用于拼接page
     */    
    public function wxMessage($wxappUserUuid,$type,$data,$params=[]){
        $ret = $this->client->connect($this->domain, 9001);
        if(!empty($ret)){
            $data = compact('wxappUserUuid','type','data','params');
            $this->send($data);
            $this->client->close();
        }
    }

    /**
     * @description: 人脸处理
     * @Author: Terry
     * @Date: 2022-05-12 15:50:15
     * @param string $wxappUserUuid 用户唯一码
     * @param string $type 发送模板的类型
     * @param array $data 模板信息
     */    
    public function randomFace($wxappUserUuid,$prjCode){
        $ret = $this->client->connect($this->domain, 9002);
        if(!empty($ret)){
            $data = compact('wxappUserUuid','prjCode');
            $this->send($data);
            $this->client->close();
        }
    }

    

    /**
     * @description: 佛山市平台处理(一个项目一个进程)
     * @Author: Terry
     * @Date: 2022-05-31 16:39:10
     * @param array $data configData [
     *          'type'
     *          'governmentPrjCode'
     *          'appid'
     *          'appSecret'
     *          'intergratorUuid'
     *          'projectCode'
     * ] 数据出自
     *@param bool  $func 处理的行为
     */    
    public function sendPullData($configData,$func,$extData=[]){
        $ret = $this->client->connect($this->domain, 9004);
        if(!empty($ret)){
            $this->send(compact('configData','func','extData'));
            $this->client->close();
        }
    }
    
    /**
     * @description: 查看南海政府平台人员是否有更新
     * @Author: Terry
     * @Date: 2022-05-31 16:39:10
     */    
    public function nanHaiPullData($configData){
        $ret = $this->client->connect($this->domain, 9003);
        if(!empty($ret)){
            $this->send($configData);
            $this->client->close();
        }
    }


    /**
     * @Description: 发送swoole请求
     * @Author: Terry
     * @Date: 2022-06-29 17:32:28
     * @param {*} $type
     * @param {*} $data
     */    
    public function sendSwoole($type,$data){
        $ret = $this->client->connect($this->domain, 9500);
        if(!empty($ret)){
            $data = compact('type','data');
            $this->send($data);
            $this->client->close();
        }
    }
}
?>